﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using log4net;
using AutoFacUtils;
using CashFlow.Web.Service;
using CashFlow.Web.MyActionResult;
using CashFlow.Web.Common;
using CashFlow.Db.Entity;
using CashFlow.Web.Filters;
using CashFlow.Web.Validator.Order;
using CashFlow.Web.Models.Order;
using CashFlow.Web.Models.Order.Result;
using CashFlow.Web.Def;

namespace CashFlow.Web.Controllers
{
    public class OrderController : Controller
    {
        private static ILog _logger = LogManager.GetLogger(typeof(OrderController));
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }
        private IOrderService GetOrderService() { return AutoFacHelper.Get<IOrderService>(); }
        private IUserAccountInfoService GetUserAccountInfoService() { return AutoFacHelper.Get<IUserAccountInfoService>(); }
        private IShopUserService GetShopUserService() { return AutoFacHelper.Get<IShopUserService>(); }

        public ActionResult Index()
        {
            var userService = GetUserService();
            var ui = userService.GetLoginUser();
            var orderService = GetOrderService();
            string date = MyCommonTool.GetOrderDate();
            var ls = orderService.QueryOrders(ui.Id, date, 1);
            ViewBag.Orders = ls;
            ViewBag.LoginUser = ui;

            return View();
        }

        [HttpPost]
        [ValidatorFilter(typeof(UploadPaymentVoucherModelValidator))]
        public ActionResult UploadPaymentVoucher(UploadPaymentVoucherModel model)
        {
            string Path = "";
            string extensionName = System.IO.Path.GetExtension(model.file.FileName);
            string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffffff") + extensionName;
            string newDir = string.Format("/UploadPaymentVoucher/{0}/", DateTime.Now.ToString("yyyy-MM-dd"));
            string newPath = string.Format("{0}{1}", newDir, newFileName);
            string path = Server.MapPath(newPath);
            string dir = Server.MapPath(newDir);
            if (!System.IO.Directory.Exists(dir))
            {
                System.IO.Directory.CreateDirectory(dir);
            }
            model.file.SaveAs(path);
            Path = newPath;

            return MyCommonTool.CreateOperationResult(true, "", Path);
        }

        [HttpPost]
        [ValidatorFilter(typeof(SavePaymentImagePathModelValidator))]
        public ActionResult SavePaymentImagePath(SavePaymentImagePathModel model)
        {
            var service = GetOrderService();
            bool bSuccess = service.UpdateUploadPeymentVoucherImagePathAndState(
                                model.OrderId
                                , model.Path
                                , (int)EOrderState.Paied
                            );
            return MyCommonTool.CreateOperationResult(bSuccess, "", null);
        }

        public ActionResult Performance()
        {
            var userService = GetUserService();
            var ui = userService.GetLoginUser();
            var orderService = GetOrderService();
            var date = MyCommonTool.GetOrderDate();
            IList<OrderBuySellInfoModel> ls = new List<OrderBuySellInfoModel>();
            if (MyCommonTool.IsRootUser())
            {
                ls = orderService.QueryAllOrders2(date);
            }
            else
            {
                ls = orderService.QueryShopAllChildUsersOrder(ui.Id, date, 1, null);
            }
            ViewBag.OrderList = ls;
            ViewBag.Date = date;

            return View();
        }

        [HttpPost]
        [ValidatorFilter(typeof(ViewSellerInfoModelValidator))]
        public ActionResult PeerInfo(ViewSellerInfoModel model)
        {
            var service = GetUserAccountInfoService();
            var entity = service.QuerySingle(x => x.UserId == model.SellerUserId);
            var entity2 = entity ?? new UserAccountInfoEntity();
            var entity3 = Convert(entity2);
            return MyCommonTool.CreateOperationResult(true, "", entity3);
        }

        [HttpPost]
        [ValidatorFilter(typeof(ReceiptPaymentModelValidator))]
        public ActionResult Receipt(ReceiptPaymentModel model)
        {
            var service = GetOrderService();
            bool bSuccess = service.UpdateOrderState(
                                model.OrderId
                                , (int)EOrderState.Recved
                            );
            return MyCommonTool.CreateOperationResult(bSuccess, "", null);
        }

        [HttpPost]
        [ValidatorFilter(typeof(PickupModelValidator))]
        public ActionResult Pickup(PickupModel model)
        {
            var service = GetOrderService();
            bool bSuccess = service.UpdateOrderState(
                                model.OrderId
                                , (int)EOrderState.Fetch
                            );
            return MyCommonTool.CreateOperationResult(bSuccess, "", null);
        }

        private UserAccountInfoModel Convert(UserAccountInfoEntity entity)
        {
            return new UserAccountInfoModel
            {
                Bank = entity.Bank,
                BankCardNo = entity.BankCardNo,
                BankAccountName = entity.BankAccountName,
                WechatPath = entity.WechatCodePath,
                AlipayPath = entity.AlipayCodePath,
                UserId = entity.UserId
            };
        }
    }
}